﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WFAExamAuto.SysManage
{
    public partial class frmUser : Form
    {
       public FrmMain fmain;
       string username;
       string userpwd;
       int userlevel;
       string realname;
       string old_username;
       string old_userpwd;
       int old_userlevel;
       string old_realname;
        public frmUser()
        {
            InitializeComponent();
        }
        public void GetUser()
        {
            dgvUserMan.AutoGenerateColumns = false;
            dgvUserMan.DataSource = fmain.cps.Pro_GetUser();
            dgvUserMan.Columns[0].DataPropertyName = "user_name";
            dgvUserMan.Columns[1].DataPropertyName = "user_password";
            dgvUserMan.Columns[2].DataPropertyName = "user_level";
            dgvUserMan.Columns[3].DataPropertyName = "user_realname";
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
           // fmain.cps.Pro_GetUserLogin("qiaoxiaorui","123456");

            dgvUserMan.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            GetUser();
            //dataGridView1.Update();
            //fmain.Ns.GetDataTable("select * from tb_User");
           
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            fmain.cps.Pro_AddUser(username, userpwd, userlevel, realname);
            GetUser();
        }

        private void dgvUserMan_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //username = dgvUserMan[0, e.Row.Index].Value.ToString();
            //userpwd = dgvUserMan[1, e.Row.Index].Value.ToString();
            //userlevel =Convert.ToInt32( dgvUserMan[2, e.Row.Index].Value.ToString());
            //realname = dgvUserMan[3, e.Row.Index].Value.ToString();
        }

        private void dgvUserMan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            username = dgvUserMan[0, e.RowIndex].Value.ToString();
            userpwd = dgvUserMan[1, e.RowIndex].Value.ToString();
            if (dgvUserMan[2, e.RowIndex].Value.ToString() != "")
            {
                userlevel = Convert.ToInt32(dgvUserMan[2, e.RowIndex].Value.ToString());
            }
            realname = dgvUserMan[3, e.RowIndex].Value.ToString();
        }

        private void dgvUserMan_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            fmain.cps.Pro_DelUser(dgvUserMan[0, dgvUserMan.CurrentRow.Index].Value.ToString());
            GetUser();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            fmain.cps.Pro_UpdateUser(old_username, realname, userlevel, userpwd, username);
        }

        private void dgvUserMan_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            old_username = dgvUserMan[0, e.RowIndex].Value.ToString();
            //old_userpwd = dgvUserMan[1, e.RowIndex].Value.ToString();
            //if (dgvUserMan[2, e.RowIndex].Value.ToString() != "")
            //{
            //    old_userlevel = Convert.ToInt32(dgvUserMan[2, e.RowIndex].Value.ToString());
            //}
            //old_realname = dgvUserMan[3, e.RowIndex].Value.ToString();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetUser();
        }
    }
}
